﻿using Asion.Dapper.SqlBuilder;
using Asion.Dapper.SqlBuilder.Condition;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static Asion.Dapper.SqlMapperExtensions;

namespace Asion.Dapper
{
    public interface ISelectColumn : ICondition
    {

    }

    public class SelectColumn : BaseCondition, ISelectColumn, IGetSql
    {
        private IList<string> Columns { get; set; }

        private bool IsDistinct { get; set; }

        public SelectColumn(string column, bool isDistinct = false)
        {
            this.Columns = new string[] { column };
            this.IsDistinct = isDistinct;
        }



        public SelectColumn(string[] columns, bool isDistinct = false)
        {
            this.Columns = columns;
            this.IsDistinct = isDistinct;
        }



        public override string BuildSql()
        {
            var stringBuilder = new StringBuilder();
            var addedAny = false;
            if (IsDistinct)
            {
                stringBuilder.Append(" distinct ");
            }
            for (var i = 0; i < Columns.Count(); i++)
            {
                var column = Columns[i];
                var property = _custom_table_mappinghandle.GetColumnMapping(column);
                if (property == null)
                {
                    throw new ArgumentException($"column that {column} does not exist ");
                }
                if (addedAny)
                    stringBuilder.Append(",");
                stringBuilder.Append(_encap_sulation.EncapSulation(property.DataColumnName) + $" as {property.ColumnName}");
                addedAny = true;
            }
            var sql = stringBuilder.ToString();

            return sql;
        }
    }

    public class AllSelectColumn : BaseCondition, ISelectColumn, IGetSql
    {


        private static ConcurrentDictionary<PropertyIdentity, string> all_sql = new ConcurrentDictionary<PropertyIdentity, string>();

        private List<string> ExcludeColumns { get; set; }

        private bool IsDistinct { get; set; }
        public AllSelectColumn(bool isDistinct=false)
        {
            this.ExcludeColumns = new List<string>();
            IsDistinct = isDistinct;

        }


        public AllSelectColumn(string[] excludes, bool isDistinct = false)
        {
            if (excludes != null && excludes.Any())
            {
                ExcludeColumns = excludes.ToList();
            }
            IsDistinct = isDistinct;
        }

        public override string BuildSql()
        {
            var identity = _custom_table_mappinghandle.GetIdentity();
            if (all_sql.ContainsKey(identity))
            {
                all_sql.TryGetValue(identity, out string result);
                return result;
            }
            var columns = _custom_table_mappinghandle.GetSelectColumn();
            var stringBuilder = new StringBuilder();
            if (IsDistinct)
            {
                stringBuilder.Append(" distinct ");
            }
            var addedAny = false;
            for (var i = 0; i < columns.Count(); i++)
            {
                var property = columns.ElementAt(i);
                //排除的列不需要展示
                if (ExcludeColumns.Contains(property.DataColumnName) || ExcludeColumns.Contains(property.ColumnName))
                {
                    continue;
                }
                if (addedAny)
                    stringBuilder.Append(",");
                stringBuilder.Append(_encap_sulation.EncapSulation(property.DataColumnName) + $" as {property.ColumnName}");
                addedAny = true;
            }
            var sql = stringBuilder.ToString();
            all_sql.TryAdd(identity, sql);
            return sql;
        }



    }




}
